package com.kugong.battle;

import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.kugong.util.NumberUtil;

import java.util.List;

/**
 *
 mysql> desc tb_battle;
 +----------+-------------+------+-----+---------+----------------+
 | Field    | Type        | Null | Key | Default | Extra          |
 +----------+-------------+------+-----+---------+----------------+
 | id       | int(11)     | NO   | PRI | NULL    | auto_increment |
 | signName | varchar(50) | YES  |     | NULL    |                |
 | state    | varchar(50) | YES  |     | NULL    |                |
 | groupNo  | varchar(50) | YES  |     | NULL    |                |
 | matchId  | varchar(50) | YES  |     | NULL    |                |
 | userId   | varchar(50) | YES  |     | NULL    |                |
 +----------+-------------+------+-----+---------+----------------+
 * Created by Administrator on 2015/8/4.
 */
public class Battle extends Model<Battle> {
    public final static Battle dao = new Battle();

    /**
     * 分页
     * @param page
     * @param size
     * @return
     */
    public Page<Battle> paginate(int page,int size){
        return paginate(page,size,"select * ", "from tb_battle  order by id desc");
    }

    /**
     * 根据比赛id查询对抗表
     * @param matchId
     * @return
     */
    public List<Battle> qryBattleByMatchId(String matchId){
        return find("select * from tb_battle where matchId = ? and state<>'弃权' order by groupNo desc,id", matchId);
    }

    /**
     * 查询下一轮对手
     * @param competitorGroupNo
     * @param matchId
     * @return
     */
    public Battle qryCompetitor(String competitorGroupNo,String matchId){
        return findFirst("select * from tb_battle where groupNo = ? and matchId = ? and state='胜利'", competitorGroupNo, matchId);
    }

    /**
     * 保存对战信息
     * @param signName
     * @param groupNo
     * @param matchId
     * @param userId
     */
    public void addBattle(String signName,String groupNo,String matchId,String userId,String state){
        Battle battle =new Battle();
        battle.set("signName",signName).set("state", state).set("groupNo",groupNo).set("matchId", matchId).set("userId",userId).save();
    }

    /**
     * 查询比赛冠军所在的组
     */
    public List<Battle> qryChampionBattle(){
        List<Battle> battles = find("SELECT max(b.groupNo) as groupNo,b.matchId,m.name FROM tb_battle b left join tb_match m on b.matchId =m.id where b.state='胜利' and m.state='已结束' group by b.matchId order by matchId desc limit 10");
        return battles;
    }

    /**
     * 查询某场比赛某组比赛的胜利者，用于查询冠军
     * @param groupNo
     * @param matchId
     * @return
     */
    public Battle qryChampion(String groupNo,String matchId){
        return findFirst("select * from tb_battle where groupNo=? and matchId=? and state='胜利'",groupNo,matchId);
    }

    /**
     * 查询下轮未比赛选手，用于改错
     * @param groupNo
     * @param matchId
     * @param userId
     * @return
     */
    public Battle qryNext(String groupNo,String matchId,String userId){
        return findFirst("SELECT * from tb_battle where groupNo=? and matchId=? and state='准备中' and userId=?",groupNo,matchId,userId);
    }

    /**
     * 查询选手是否已经在比赛中
     * @param matchId
     * @param userId
     * @return
     */
    public Battle qryOnlyOne(String matchId,String userId){
        return findFirst("SELECT * from tb_battle where matchId=? and state='准备中' and userId=?",matchId,userId);
    }
}
